/*
 * ObjectPropertyPanel.java
 *
 * Created on 13. Juni 2008, 20:14
 */

package twi.view.manipulation;

import javax.swing.DefaultListModel;

import twi.Utils;
import twi_project.ErrorDialog;
import twi_project.IndividualAdding;
import twi_project.TWI;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

/**
 *
 * @author  Soeren
 */
public class ObjectPropertyPanel extends javax.swing.JPanel {

	
    private OntProperty property;
	private CreateIndividualPanel parentFrame;
	
    /** Creates new form ObjectPropertyPanel */
    public ObjectPropertyPanel(OntProperty ontProperty, Individual indi, CreateIndividualPanel parent) {
    	this.parentFrame = parent;
        initComponents();
        this.property = ontProperty;
        this.jLabel2.setText(this.property.getLocalName());
        setBorder(javax.swing.BorderFactory.createTitledBorder("Object Property - "+this.jLabel2.getText()));
        this.jList_individuals.setModel(new DefaultListModel());
        this.jList_range.setModel(new DefaultListModel());
        this.jList_values.setModel(new DefaultListModel());
        
        setup();
        
        if (indi != null){
        	RDFNode valueRDF = indi.getPropertyValue(ontProperty);
        	String[] split = Utils.getLocalName(valueRDF).split(";");
        	for (int i = 0; i < split.length; i++){
        		((DefaultListModel)this.jList_values.getModel()).addElement(split[i]);
        	}
        	
        }
    }
    

	public void updateModifications() {
		setup();
	}
    
    private void setup() {
    	((DefaultListModel)this.jList_range.getModel()).removeAllElements();
		String[] split = Utils.getLocalName(property.getRange()).split(";");
		for (int i = 0;i < split.length; i++)
			((DefaultListModel)this.jList_range.getModel()).addElement(split[i]);
		this.jList_range.setSelectedIndex(0);
	}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList_range = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList_individuals = new javax.swing.JList();
        jButton_add = new javax.swing.JButton();
        jButton_del = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList_values = new javax.swing.JList();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(twi_project.TWI_ProjectApp.class).getContext().getResourceMap(ObjectPropertyPanel.class);
        setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("Form.border.title"))); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList_range.setBorder(javax.swing.BorderFactory.createTitledBorder("Range"));
        jList_range.setName("jList_range"); // NOI18N
        jList_range.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList_rangeValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList_range);

        jScrollPane2.setBorder(null);
        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jList_individuals.setBorder(javax.swing.BorderFactory.createTitledBorder("Individuals"));
        jList_individuals.setName("jList_individuals"); // NOI18N
        jList_individuals.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList_individualsValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jList_individuals);

        jButton_add.setText(resourceMap.getString("jButton_add.text")); // NOI18N
        jButton_add.setName("jButton_add"); // NOI18N
        jButton_add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_addActionPerformed(evt);
            }
        });

        jButton_del.setText(resourceMap.getString("jButton_del.text")); // NOI18N
        jButton_del.setName("jButton_del"); // NOI18N
        jButton_del.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_delActionPerformed(evt);
            }
        });

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jList_values.setBorder(javax.swing.BorderFactory.createTitledBorder("values"));
        jList_values.setName("jList_values"); // NOI18N
        jList_values.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList_valuesValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(jList_values);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton_del, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton_add, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addComponent(jButton_add)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton_del)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE))))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jList_rangeValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList_rangeValueChanged
OntClass oc = TWI.getInstance().getOntologieModel().getOntClass((String) this.jList_range.getSelectedValue());
	if (oc == null)
		return ;
	((DefaultListModel)this.jList_individuals.getModel()).removeAllElements();	
	ExtendedIterator iter = oc.listInstances();
	while(iter.hasNext()){
		Individual in = (Individual) iter.next();
		((DefaultListModel)this.jList_individuals.getModel()).addElement(Utils.getLocalName(in));
	}
}//GEN-LAST:event_jList_rangeValueChanged

private void jList_individualsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList_individualsValueChanged

}//GEN-LAST:event_jList_individualsValueChanged

private void jButton_addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_addActionPerformed
String val = (String) this.jList_individuals.getSelectedValue();
	if (val == null)
		return ;
	((DefaultListModel)this.jList_values.getModel()).addElement(val);
}//GEN-LAST:event_jButton_addActionPerformed

private void jButton_delActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_delActionPerformed
int idx = this.jList_values.getSelectedIndex();
	if (idx < 0)
		return ;
	((DefaultListModel)this.jList_values.getModel()).remove(idx);
}//GEN-LAST:event_jButton_delActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
OntClass oc = TWI.getInstance().getOntologieModel().getOntClass((String) this.jList_range.getSelectedValue());
	if (oc == null){
		ErrorDialog ed = new ErrorDialog(null, false, "you should select at least the Class");
		ed.setVisible(true);
		return ;
	}
	TWI.getInstance().createNewInstance(oc.getLocalName(), parentFrame);
}//GEN-LAST:event_jButton1ActionPerformed

private void jList_valuesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList_valuesValueChanged

}//GEN-LAST:event_jList_valuesValueChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton_add;
    private javax.swing.JButton jButton_del;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList_individuals;
    private javax.swing.JList jList_range;
    private javax.swing.JList jList_values;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables
	public void commit(String individualName) {
		String propertyName = this.property.getLocalName();
		DefaultListModel model = (DefaultListModel) this.jList_values.getModel();
		for (int i = 0; i < model.getSize(); i++){
			String value = (String) model.get(i);
			IndividualAdding.addProperty(propertyName, value, individualName);
		}
	}


	public void tryResourceAsIndividual(Individual indi) {
		OntClass oc = indi.getOntClass();
		String n = Utils.getLocalName(oc);
		DefaultListModel dlm = (DefaultListModel)this.jList_range.getModel();
		for (int i = 0; i < dlm.getSize(); i++)
			if (dlm.getElementAt(i).equals(n)){
				((DefaultListModel)jList_values.getModel()).addElement(indi.getLocalName());
				validate();
				break;
			}
	}

}
